package org.ws.lifebacklog.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * @author sergey.valuy
 */
public class GenericAdapter<T> extends BaseAdapter {

	protected LayoutInflater inflater;
	protected ViewBinder<T> viewBinder;
	private List<T> items;

	public GenericAdapter(Context context, ViewBinder<T> viewBinder, List<T> items) {
		inflater = LayoutInflater.from(context);
		this.viewBinder = viewBinder;
		this.items = items;
	}

	/**
	 * @return the items
	 */
	public final List<T> getItems() {
		return items;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public T getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView = convertView;
		if (itemView == null) {
			itemView = inflater.inflate(viewBinder.getViewLayoutId(), null);
		}
		viewBinder.bindView(itemView, getItem(position), position);
		return itemView;
	}

}
